package com.pai4j.client.service;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.exception.BusinessException;
import com.pai4j.domain.vo.request.request.MomentSaveOrUpdateVO;
import com.pai4j.domain.vo.response.moment.MomentDetailVO;
import com.pai4j.remote.ugc.MomentClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class MomentService {


    @Autowired
    private MomentClient momentClient;

    /**
     * 发布动态
     * @param momentSaveOrUpdateVO
     * @return
     */
    public Long publish(MomentSaveOrUpdateVO momentSaveOrUpdateVO) {

        // RPC 请求UGC微服务实现朋友圈动态发布核心流程
        PAIResponseBean<Long> responseBean = momentClient.publish(momentSaveOrUpdateVO);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMessage());
    }

    /**
     * 朋友圈动态列表加载
     *
     * @param userId
     * @param lastMomentId
     * @param size
     * @return
     */
    public List<MomentDetailVO> timeline(String userId, Long lastMomentId, int size) {

        PAIResponseBean<List<MomentDetailVO>> responseBean = momentClient.timeline(userId, lastMomentId, size);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return Collections.emptyList();
    }
}
